<?php
/**
 * User: Jerry
 * Date: 2017/7/28
 * Time: 10:13
 */

namespace SDXApi\Repositories;


use SDXApi\Service\AlbumService;

/**
 * Class DiscoverGoodsRepository
 * @author Jerry
 * @package SDXApi\Repositories
 */
class DiscoverGoodsRepository
{
    /**
     * @var AlbumService
     */
    protected $albumService;

    /**
     * DiscoverGoodsRepository constructor.
     */
    public function __construct()
    {
        $this->albumService = new AlbumService();
    }

    /**
     * @author Jerry
     * @param int $orientation
     * @return array
     */
    public function handleOrientationHome($orientation = 0)
    {
        $albums = array();

        $number = 0;
        if ($orientation == 1) {
            $number = 12;
        }

        if ($orientation == 2) {
            $number = 2 * 100;
        }

        if ($orientation == 3) {
            $albums = $this->albumService->getArticleAlbums();
            $number = count($albums) * 7;
        }

        $result = array(
            'albums_data' => $albums,
            'number'      => $number,
        );

        return $result;
    }

    /**
     * @author Jerry
     * @param array $goods
     * @param array $album
     * @return array|bool
     */
    public function handleOrientationHomeGoods($goods = array(), $album = array())
    {
        if (empty($goods)) {
            return false;
        }

        $data = array();
        if (empty($album)) {
            $data['type']   = 1;
            $data['goods_data']   = $goods;

            return $data;
        }

        $chunkData = array_chunk($goods, 7);

        foreach ($album as $key => $value) {
            if (! empty($chunkData[$key])) {
                $data[] = array(
                    'type'  =>  1,
                    'goods_data'  =>  array_slice($chunkData[$key], 0, 1),
                );
            }

            $data[] = array(
                'type'  =>  2,
                'album_data'  =>  $value,
            );

            if (! empty($chunkData[$key])) {
                $data[] = array(
                    'type'  =>  3,
                    'goods_data'  =>  array_slice($chunkData[$key], 1, 6),
                );
            }

        }

        return $data;
    }
}